using System;

namespace Mbs
{
    [Serializable]
    public struct DisposeAction : IDisposable
    {
        private struct EmptyDisposeAction : IDisposable
        {
            public void Dispose() { }
        }

        public static readonly IDisposable Empty = new EmptyDisposeAction();

        private Object Owner;
        private Action Action;
        public DisposeAction(Action action)
        {
            Action = action;
            Owner = null;
        }

        public DisposeAction(object owner, Action action)
        {
            Owner = owner;
            Action = action;
        }

        public void Dispose()
        {
            if (Owner != null)
                ServiceRegistry.Current.UnRegister(Owner);
            if (Action != null)
                Action();
        }
    }
}
